package chatRoom.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * 读取服务器发送来的信息
 */
public class ServerHandler implements Runnable{
    private Socket socket;
    private String host;
    public ServerHandler(Socket socket) {
        this.socket=socket;
        host=socket.getInetAddress().getCanonicalHostName();
    }

    @Override
    public void run() {
        try(BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream(),"utf-8"))) {
            String message;
            while ((message=br.readLine())!=null){
                String[]sub=message.split(":");
                if(!host.equals(sub[0])) {
                    System.out.println(message);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
